// +----------------------------------------------------------------------
// | Project:   boxfish-android-commons  
// +----------------------------------------------------------------------
// | CreateTime: 15/12/2  上午10:55
// +----------------------------------------------------------------------
// | Author:     xab(xab@xabad.cn)
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
package cn.xabad.commons.sample.tools;

import cn.xabad.commons.io.IOUtils;
import cn.xabad.commons.sample.model.DownResponseBody;
import cn.xabad.commons.tools.FileU;
import cn.xabad.commons.tools.MD5;
import cn.xabad.commons.tools.StringU;
import com.google.common.collect.Lists;
import com.google.gson.stream.JsonReader;
import com.squareup.okhttp.ResponseBody;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * DESC   :
 * AUTHOR : Xabad
 */
public class AssetsU {

    public static List<String> getFileList() {
        List<String> fileList = Lists.newArrayList();
        FileReader reader = null;
        JsonReader jsonReader = null;
        try {
            String filePath = FilePathU.getStaticResByName("res.json");
            File file = new File(filePath);
            reader = new FileReader(file);
            jsonReader = new JsonReader(reader);
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                String name = jsonReader.nextName();
                if (StringU.equals(name, "files")) {
                    jsonReader.beginArray();
                    while (jsonReader.hasNext()) {
                        jsonReader.beginObject();
                        jsonReader.nextName();
                        String filename = jsonReader.nextString();
                        jsonReader.nextName();
                        String filemd5 = jsonReader.nextString();

                        if (!skiped(filename)) {
                            if (!MD5.checkFileMd5(FilePathU.getStaticResByName(filename), filemd5)) {
                                fileList.add(filename);
                            }
                        }
                        jsonReader.endObject();
                    }
                    jsonReader.endArray();
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            IOUtils.closeQuietly(jsonReader);
            IOUtils.closeQuietly(reader);
        }
        return fileList;
    }

    public static boolean skiped(String name) {
        return StringU.startsWithAny(name, "tag/", "knowledge/");
    }

    public static void saveFile(DownResponseBody responseBody) throws IOException {
        if (responseBody == null) {
            throw new IOException("DownResponseBody can not be null ");
        }
        ResponseBody body = responseBody.getResponseBody();
        if (body == null) {
            throw new IOException("responseBody can not be null ");
        }

        if (StringU.isEmpty(responseBody.getFileName())) {
            throw new IOException("FileName can not be null ");
        }
        saveFile(responseBody.getFileName(), body.byteStream());
    }

    public static void saveFile(String fileName, InputStream stream) throws IOException {
        String filePath = FilePathU.getStaticResByName(fileName);
        FileU.ifNotExistCreateFile(filePath);
        FileU.writeStreamToFile(filePath, stream);
    }
}
